document.addEventListener('DOMContentLoaded', function() {
    // 选项卡切换
    const homeTab = document.getElementById('home-tab');
    const portfolioTab = document.getElementById('portfolio-tab');
    const pcHomeTab = document.getElementById('pc-home-tab');
    const pcPortfolioTab = document.getElementById('pc-portfolio-tab');
    
    const homeSection = document.querySelector('.home-section');
    const portfolioSection = document.querySelector('.portfolio-section');
    
    function switchTab(activeTab, inactiveTab, activeSection, inactiveSection) {
        // 更新移动端菜单状态
        if (homeTab) homeTab.classList.toggle('active', activeTab === homeTab || activeTab === pcHomeTab);
        if (portfolioTab) portfolioTab.classList.toggle('active', activeTab === portfolioTab || activeTab === pcPortfolioTab);
        
        // 更新PC端菜单状态
        if (pcHomeTab) pcHomeTab.classList.toggle('active', activeTab === homeTab || activeTab === pcHomeTab);
        if (pcPortfolioTab) pcPortfolioTab.classList.toggle('active', activeTab === portfolioTab || activeTab === pcPortfolioTab);
        
        activeSection.style.display = 'block';
        inactiveSection.style.display = 'none';
        window.scrollTo(0, 0);
    }
    
    // 移动端菜单事件
    if (homeTab) homeTab.addEventListener('click', () => switchTab(homeTab, portfolioTab, homeSection, portfolioSection));
    if (portfolioTab) portfolioTab.addEventListener('click', () => switchTab(portfolioTab, homeTab, portfolioSection, homeSection));
    
    // PC端菜单事件
    if (pcHomeTab) pcHomeTab.addEventListener('click', (e) => {
        e.preventDefault();
        switchTab(pcHomeTab, pcPortfolioTab, homeSection, portfolioSection);
    });
    
    if (pcPortfolioTab) pcPortfolioTab.addEventListener('click', (e) => {
        e.preventDefault();
        switchTab(pcPortfolioTab, pcHomeTab, portfolioSection, homeSection);
    });
    
    // 联系咨询弹窗
    const contactModal = document.getElementById('contact-modal');
    const heroContactBtn = document.getElementById('hero-contact-btn');
    const closeModal = document.getElementById('close-modal');
    const contactForm = document.getElementById('contact-form');
    
    function openContactModal() {
        contactModal.style.display = 'flex';
        document.body.style.overflow = 'hidden';
    }
    
    if (heroContactBtn) {
        heroContactBtn.addEventListener('click', function(e) {
            e.preventDefault();
            openContactModal();
        });
    }
    
    function closeContactModal() {
        contactModal.style.display = 'none';
        document.body.style.overflow = '';
    }
    
    if (closeModal) {
        closeModal.addEventListener('click', closeContactModal);
    }
    
    window.addEventListener('click', function(event) {
        if (event.target === contactModal) {
            closeContactModal();
        }
    });
    
//  // 表单提交到企业微信
// if (contactForm) {
//     contactForm.addEventListener('submit', function(e) {
//         e.preventDefault();
        
//         // 验证所有必填字段
//         const requiredFields = contactForm.querySelectorAll('[required]');
//         let isValid = true;
        
//         requiredFields.forEach(field => {
//             if (!field.value.trim()) {
//                 field.classList.add('invalid');
//                 isValid = false;
//             } else {
//                 field.classList.remove('invalid');
//             }
//         });
        
//         // 验证手机号格式
//         const phoneField = document.getElementById('phone');
//         if (phoneField && !/^1[3-9]\d{9}$/.test(phoneField.value.trim())) {
//             phoneField.classList.add('invalid');
//             isValid = false;
//             alert('请输入正确的手机号码');
//             return;
//         }
        
//         if (!isValid) {
//             alert('请填写所有必填字段');
//             return;
//         }
        
//         // 收集表单数据
//         const formData = {
//             name: document.getElementById('name').value.trim(),
//             phone: document.getElementById('phone').value.trim(),
//             area: document.getElementById('area').value.trim(),
//             budget: document.getElementById('budget').value.trim(),
//             type: document.getElementById('type').value.trim(),
//             timestamp: new Date().toLocaleString()
//         };
        
//         // 显示提交中的提示
//         showLoading('正在提交中，请稍候...');
        
//         // 提交到企业微信
//         submitToWeCom(formData);
//     });
// }

// // 显示加载中的弹窗
// function showLoading(message) {
//     const loadingDiv = document.createElement('div');
//     loadingDiv.id = 'loading-modal';
//     loadingDiv.style.position = 'fixed';
//     loadingDiv.style.top = '0';
//     loadingDiv.style.left = '0';
//     loadingDiv.style.width = '100%';
//     loadingDiv.style.height = '100%';
//     loadingDiv.style.backgroundColor = 'rgba(0,0,0,0.5)';
//     loadingDiv.style.display = 'flex';
//     loadingDiv.style.justifyContent = 'center';
//     loadingDiv.style.alignItems = 'center';
//     loadingDiv.style.zIndex = '9999';
    
//     const contentDiv = document.createElement('div');
//     contentDiv.style.backgroundColor = 'white';
//     contentDiv.style.padding = '20px';
//     contentDiv.style.borderRadius = '5px';
//     contentDiv.style.textAlign = 'center';
    
//     const spinnerDiv = document.createElement('div');
//     spinnerDiv.style.border = '4px solid #f3f3f3';
//     spinnerDiv.style.borderTop = '4px solid #3498db';
//     spinnerDiv.style.borderRadius = '50%';
//     spinnerDiv.style.width = '30px';
//     spinnerDiv.style.height = '30px';
//     spinnerDiv.style.animation = 'spin 1s linear infinite';
//     spinnerDiv.style.margin = '0 auto 10px';
    
//     const messageP = document.createElement('p');
//     messageP.textContent = message;
//     messageP.style.margin = '0';
    
//     // 添加CSS动画
//     const style = document.createElement('style');
//     style.textContent = `
//         @keyframes spin {
//             0% { transform: rotate(0deg); }
//             100% { transform: rotate(360deg); }
//         }
//     `;
    
//     contentDiv.appendChild(spinnerDiv);
//     contentDiv.appendChild(messageP);
//     loadingDiv.appendChild(contentDiv);
//     document.body.appendChild(style);
//     document.body.appendChild(loadingDiv);
// }

// // 隐藏加载中的弹窗
// function hideLoading() {
//     const loadingDiv = document.getElementById('loading-modal');
//     if (loadingDiv) {
//         document.body.removeChild(loadingDiv);
//     }
// }

// // 显示结果弹窗
// function showResultModal(message, isSuccess) {
//     hideLoading(); // 先隐藏加载中的弹窗
    
//     const modalDiv = document.createElement('div');
//     modalDiv.id = 'result-modal';
//     modalDiv.style.position = 'fixed';
//     modalDiv.style.top = '0';
//     modalDiv.style.left = '0';
//     modalDiv.style.width = '100%';
//     modalDiv.style.height = '100%';
//     modalDiv.style.backgroundColor = 'rgba(0,0,0,0.5)';
//     modalDiv.style.display = 'flex';
//     modalDiv.style.justifyContent = 'center';
//     modalDiv.style.alignItems = 'center';
//     modalDiv.style.zIndex = '9999';
    
//     const contentDiv = document.createElement('div');
//     contentDiv.style.backgroundColor = 'white';
//     contentDiv.style.padding = '20px';
//     contentDiv.style.borderRadius = '5px';
//     contentDiv.style.textAlign = 'center';
//     contentDiv.style.maxWidth = '80%';
    
//     const iconDiv = document.createElement('div');
//     iconDiv.style.fontSize = '40px';
//     iconDiv.style.marginBottom = '10px';
//     iconDiv.textContent = isSuccess ? '✓' : '✗';
//     iconDiv.style.color = isSuccess ? '#4CAF50' : '#F44336';
    
//     const messageP = document.createElement('p');
//     messageP.textContent = message;
//     messageP.style.margin = '0 0 20px';
    
//     const closeButton = document.createElement('button');
//     closeButton.textContent = '确定';
//     closeButton.style.padding = '8px 20px';
//     closeButton.style.backgroundColor = isSuccess ? '#4CAF50' : '#F44336';
//     closeButton.style.color = 'white';
//     closeButton.style.border = 'none';
//     closeButton.style.borderRadius = '4px';
//     closeButton.style.cursor = 'pointer';
    
//     closeButton.addEventListener('click', function() {
//         document.body.removeChild(modalDiv);
//     });
    
//     contentDiv.appendChild(iconDiv);
//     contentDiv.appendChild(messageP);
//     contentDiv.appendChild(closeButton);
//     modalDiv.appendChild(contentDiv);
//     document.body.appendChild(modalDiv);
// }

// // 提交到企业微信函数
// function submitToWeCom(data) {
//     // 替换为您企业微信机器人的实际Webhook URL
//     const webhookUrl = 'http://1366618611-eq2n6fums9.ap-guangzhou.tencentscf.com';
    
//     // 构建消息内容
//     const messageContent = `新的装修需求:
// 姓名: ${data.name}
// 电话: ${data.phone}
// 面积: ${data.area}平米
// 预算: ${data.budget}元
// 类型: ${data.type}
// 提交时间: ${data.timestamp}`;
    
//     // 使用fetch发送请求
//     fetch(webhookUrl, {
//         method: 'POST',
//         headers: {
//             'Content-Type': 'application/json'
//         },
//         body: JSON.stringify({
//             msgtype: "text",
//             text: {
//                 content: messageContent
//             }
//         })
//     })
//     .then(response => {
//         if (!response.ok) {
//             throw new Error('网络响应不正常');
//         }
//         return response.json();
//     })
//     .then(data => {
//         if (data.errcode === 0) {
//             // 提交成功
//             showResultModal('提交成功！我们的设计师将尽快联系您。', true);
//             contactForm.reset();
//             closeContactModal();
//         } else {
//             // 企业微信返回错误
//             throw new Error(data.errmsg || '提交失败');
//         }
//     })
//     .catch(error => {
//         console.error('提交失败:', error);
//         showResultModal('提交失败，请稍后重试或直接拨打我们的电话。', false);
//     });
// }

    
    // 视频全屏功能
    const videoModal = document.getElementById('video-modal');
    const closeVideoModal = document.getElementById('close-video-modal');
    const fullscreenVideo = document.getElementById('fullscreen-video');
    const videoModalTitle = document.getElementById('video-modal-title');
    const videoModalDesc = document.getElementById('video-modal-desc');
    
    document.querySelectorAll('.portfolio-item').forEach(item => {
        item.addEventListener('click', function() {
            const videoSrc = this.getAttribute('data-video');
            const title = this.getAttribute('data-title');
            const desc = this.getAttribute('data-desc');
            
            fullscreenVideo.src = videoSrc;
            videoModalTitle.textContent = title;
            videoModalDesc.textContent = desc;
            
            videoModal.style.display = 'flex';
            document.body.style.overflow = 'hidden';
            fullscreenVideo.play();
        });
    });
    
    function closeVideoModalFunc() {
        videoModal.style.display = 'none';
        document.body.style.overflow = '';
        fullscreenVideo.pause();
    }
    
    if (closeVideoModal) {
        closeVideoModal.addEventListener('click', closeVideoModalFunc);
    }
    
    window.addEventListener('click', function(event) {
        if (event.target === videoModal) {
            closeVideoModalFunc();
        }
    });
    
    // ESC键关闭弹窗
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') {
            if (contactModal.style.display === 'flex') {
                closeContactModal();
            }
            if (videoModal.style.display === 'flex') {
                closeVideoModalFunc();
            }
        }
    });
    
    // 默认显示首页
    if (homeSection) homeSection.style.display = 'block';
    if (portfolioSection) portfolioSection.style.display = 'none';
});